package cn.rxls.community.service.impl;

import cn.rxls.community.dto.PageDTO;
import cn.rxls.community.entity.Activity;
import cn.rxls.community.entity.Category;
import cn.rxls.community.mapper.ActivityMapper;
import cn.rxls.community.mapper.CategoryMapper;
import cn.rxls.community.service.ActivityService;
import cn.rxls.community.vo.activity.ActivityPageVO;
import cn.rxls.community.vo.activity.ActivityVO;
import cn.rxls.community.vo.category.CategoryVO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 活动(Activity)表服务实现类
 *
 */
@Service("activityService")
@Transactional
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements ActivityService {

    private final ActivityMapper activityMapper;

    private final CategoryMapper categoryMapper;

    ActivityServiceImpl(ActivityMapper activityMapper, CategoryMapper categoryMapper) {
        this.activityMapper = activityMapper;
        this.categoryMapper = categoryMapper;
    }


    /**
     * 根据分类分页查询数据
     *
     * @param pageDTO      分页参数
     * @param activity 参数条件对象
     * @return 多个活动数据
     */
    @Override
    public ActivityPageVO selectPageActivityCategory(PageDTO pageDTO, Activity activity) {
        //返回的对象
        ActivityPageVO activityPageVO = new ActivityPageVO();

        //查询总数
        Integer total = activityMapper.selectActivityTotal(activity);
        //如果总数为0,直接结束
        if (Objects.isNull(total) || total.equals(0)) {
            activityPageVO.setActivity(new ArrayList<>());
            activityPageVO.setTotal(0);
            return activityPageVO;
        }
        //查询列表数据
        List<ActivityVO> activityVO = activityMapper.selectActivityPage(pageDTO.getPage(), pageDTO.getSize(), activity);
        //封裝返回的数据
        activityPageVO.setActivity(activityVO);
        activityPageVO.setTotal(total);
        return activityPageVO;
    }




    /**
     * 根据活动id查询相关数据
     *
     * @param id 活动id
     * @return 活动详情数据
     */
    @Override
    public ActivityVO selectActivityDetail(Long id) {

        //开始查询

        return activityMapper.selectActivityById(id);
    }

    /**
     * 获取所有的活动分类
     *
     * @return 多个活动类型数据
     */
    @Override
    public List<CategoryVO> selectActivityAllCategory() {
        //查询所有的分类
        List<Category> category = categoryMapper.selectList(null);
        //封装返回数据
        List<CategoryVO> categoryVOS = new ArrayList<>();
        category.forEach(s -> {
            CategoryVO categoryVO = new CategoryVO();
            BeanUtils.copyProperties(s,categoryVO);
            categoryVOS.add(categoryVO);
        });
        return categoryVOS;
    }
}

